/*
 * 系统名称: 统一接入
 * 模块名称: access-client
 * 文件名: MapUtils.java
 * 包名: com.bozhong.access.utils
 * 修改说明:
 * 修改日期                     修改人员                       修改说明
 * ==================================================================================
 *  2017年2月7日                     xiongxin                        创建
 *
 * ==================================================================================
*/
package com.alpaak.common.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;
import java.util.Map.Entry;

/**
 * 模块说明: Map工具类 
 * 开发人员: xiongxin 
 * 开发时间: 2017年2月7日 
 * 系统版本: v1.0
 */
public class MapUtils {

	/**
	 * 移除Map中指定的key 
	 * 
	 * @param map
	 * @param arr
	 * @return
	 */
	public static void removeKeys(Map<String, Object> map, String[] arr) {
		for (int i = 0; i < arr.length; i++) {
			if (map.containsKey(arr[i]))
				map.remove(arr[i]);
		}
	}
	/**
	 * 替换Map中指定的key
	 * 
	 * @param map
	 * @param arr
	 * @return
	 */
	public static void replaceKeys(Map<String, Object> map, String oldKey,String newKey) {
		if(map.containsKey(oldKey)){
			Object value=map.get(oldKey);
			map.remove(oldKey);
			map.put(newKey, value);
		}
	}
	@SuppressWarnings("rawtypes")
	public static String getValueByKey(Map map, Object key) {
		String values = "";

		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Entry entry = (Entry) it.next();
			Object obj = entry.getKey();
			if (obj != null && obj.equals(key)) {
				values = (String) entry.getValue();
			}

		}
		return values;
	}

	// 根据Value取Key
	@SuppressWarnings("rawtypes")
	public static String getKeyByValue(Map map, Object value) {
		String keys = "";
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Entry entry = (Entry) it.next();
			Object obj = entry.getValue();
			if (obj != null && obj.equals(value)) {
				keys = (String) entry.getKey();
			}

		}
		return keys;
	}

	@SuppressWarnings("rawtypes")
	public static Map<String, Object> toParameterMap1(Map<String, Object> parameter) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (Iterator iter = parameter.entrySet().iterator(); iter.hasNext();) {
			Entry element = (Entry) iter.next();
			// key值
			String strKey = (String) element.getKey();
			// value,数组形式
			String[] value = (String[]) element.getValue();
			for (int i = 0; i < value.length; i++) {
				map.put(strKey, value[i]);
			}
		}
		return map;
	}
	@SuppressWarnings("rawtypes")
	public static Map<String, Object> toParameterMap(Map<String, Object> parameter) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (Iterator iter = parameter.entrySet().iterator(); iter.hasNext();) {
			Entry element = (Entry) iter.next();
			// key值
			String strKey = (String) element.getKey();
			// value,数组形式
			String[] value = (String[]) element.getValue();
			if("args".equals(strKey) && value != null && value.length > 0){
				String param = value[0];
				/*try {
					param = new String(param.getBytes("ISO8859-1"),"utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
				JSONObject o = JSONObject.fromObject(param);
				if(o != null){
					map = parseJSON2Map(o);
				}
			}
		}
		return map;
	}
	/**
	 * JSON转Map
	 * @param json
	 * @return
	 */
	private static Map<String, Object> parseJSON2Map(JSONObject json) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 最外层解析
        for (Object k : json.keySet()) {
            Object v = json.get(k);
            // 如果内层还是数组的话，继续解析
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                Iterator<JSONObject> it = ((JSONArray) v).iterator();
                while (it.hasNext()) {
                    JSONObject json2 = it.next();
                    list.add(parseJSON2Map(json2));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }
}